<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Report.php");
require_once("../../data/Item.php");

/**
 * Display all the reportItems associated with this activity
 * @param Activity $p_record
 */
function displayAllReportItems($p_record){
	?>
	<a href="display_all_activity_reports.php?id=<?=base64_encode($p_record->getId()) ?>">Display all reports</a>
	<?php
}

/*
A request parameter which (if equals to 1) filter the "inactive" activitie.
That is to say the activities which are not associated with an open action or with a recent report item.
*/
define(ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER,"active");

SessionManager::getInstance()->checkSession();
displayTop("Global report per activity","../../resources/ajaxscripts.php",null);

if($_REQUEST[ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER]!="1"){
	?>
	<p><a href="?<?=ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER ?>=1">Display only the "active" activities</a></p>
	<?php
}else{
	?>
	<p><a href="?<?=ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER ?>=0">Display all the activities</a></p>
	<?php
}

$l_myUserId=SessionManager::getCurrentLoggedUserId();
$l_user=User::getFromPrimaryKey($l_myUserId);
$l_results=$l_user->getFullTeam();

$l_teamActivities=array();
if(count($l_results)!=0){
	foreach($l_results as $l_teamMember){
		if($_REQUEST[ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER]=="1"){
			$l_teamActivities=array_merge($l_teamActivities,$l_teamMember->getOpenAndActiveActivities());
		}else{
			$l_teamActivities=array_merge($l_teamActivities,$l_teamMember->getOpenActivities());
		}
	}
}
$l_teamActivities=array_merge($l_teamActivities,$l_user->getOpenActivities());

if(count($l_teamActivities)!=0){
	$l_teamActivitiesTable=new TableData("ActivitiesTable");
	$l_teamActivitiesTable->addHeader("title","Title");
	$l_teamActivitiesTable->addAction("displayAllReportItems");
	$l_teamActivitiesTable->setRecords($l_teamActivities);
	$l_teamActivitiesTable->setZoomArea("ajax_per_activity_dashboard_details.php");
	echo "<h1>The activities managed by your team</h1>";
	displayTable($l_teamActivitiesTable);
}else{
	?>
	<div style="color: white;background-color: red;">No activities</div>
	<?php
}
?>
<center>
<a target="_blank" href="print_per_activity_dashboard.php?<?=ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER ?>=<?=$_REQUEST[ACTIVE_ACTIVITIES_ONLY_REQUEST_PARAMETER] ?>"><img border="0" src="<?=ROOT_PATH ?>/apps/resources/images/print.png"/></a>
<br/>
<i>Print this report (this will open a new window)</i>
</center> 
<?php
displayBottom();
?>